//
//  BSRegisterViewController.m
//  SuoSiApp
//
//  Created by zhang on 16/4/5.
//  Copyright © 2016年 Ym. All rights reserved.
//

#import "StringResponse.h"

#import "BSUserProtocolInfromationView.h"

#import "BSRegisterViewController.h"


NSInteger num = 60;

@interface BSRegisterViewController ()<UIAlertViewDelegate>

@property (nonatomic, assign) BOOL isAgreePro;

@end

@implementation BSRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = BSWhite;
    self.registView = [[BSRegistView alloc]initWithFrame:self.view.bounds];
    
    [self.registView.getVerificatBT addTarget:self action:@selector(getVerification) forControlEvents:(UIControlEventTouchUpInside)];
    [self.registView.agreenBT addTarget:self action:@selector(agreenPro) forControlEvents:(UIControlEventTouchUpInside)];
    [self.registView.registNowBT addTarget:self action:@selector(registNow) forControlEvents:(UIControlEventTouchUpInside)];
    [self.registView.protocolBT addTarget:self action:@selector(lookUserProtocol) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview: _registView];
    
    
    UIButton *button = BSButton;
    button.frame = CGRectMake(20, 25, 40, 30);
//    button.backgroundColor = BSRed;
    [button setTitle:@"返回" forState:(UIControlStateNormal)];
    [button setTitleColor:BSBlack forState:(UIControlStateNormal)];
    
    [button addTarget:self action:@selector(aaaa) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];
    
}
- (void)agreenPro{

    if (_isAgreePro == YES) {
             [self.registView.agreenBT setImage:[UIImage imageNamed:@"register_notSelectProtoco.png"] forState:(UIControlStateNormal)];
      _isAgreePro = NO;
        
        self.registView.registNowBT.enabled = NO;

        
    } else {
        [self.registView.agreenBT setImage:[UIImage imageNamed:@"register_chk_nor.png"] forState:(UIControlStateNormal)];
        
        _isAgreePro = YES;
   
        self.registView.registNowBT.enabled = YES;
    }
}

//返回登陆界面
- (void)aaaa{


    
    [_changeTimer invalidate];
    _changeTimer = nil;

    [self dismissViewControllerAnimated:YES completion:^{
        nil;
    }];
}

// 获取验证码
- (void)getVerification{
    
    if (self.registView.accountTF.text.length == 11 ) {
        [PKRequestManager GetRegisterVerifyCodeWithnetUrl:NetStr loginName:self.registView.accountTF.text method:POST finish:^(NSData *data) {
            NSLog(@"请求验证码");
            StringResponse *strResponseModel = [[StringResponse alloc]init];
            [strResponseModel dealWithData:data];
            
            
            
        } error:^(NSError *error) {
            ;
        }];
        
        _changeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
        

    }else{
    
        [PKRequestManager showAlertView:@"账号有误"];
    
    }
    
  

   }

// 定时器 
- (void)timeChange{
    
    self.registView.getVerificatBT.enabled = NO;
    
    if (num != 0) {
        num = num - 1;
        self.registView.timeLB.text = [NSString stringWithFormat:@"%ld%@",(long)num,@"s"];

    }else{
    
        num = 60;
        self.registView.getVerificatBT.enabled = YES;
 self.registView.timeLB.text = [NSString stringWithFormat:@"%ld%@",(long)num,@"s"];
        
        [_changeTimer setFireDate:[NSDate distantFuture]];
    }
   }

//提交注册
- (void)registNow{


    

    if ([self.registView.passwordTF.text isEqualToString:self.registView.confirmPasswordTF.text]) {
        
        if (self.registView.passwordTF.text.length < 8) {
            [PKRequestManager showAlertView:@"密码必须为8位数字"];
            return;
        }
        
        
        [PKRequestManager RegistWithnetUrl:NetStr loginName:self.registView.accountTF.text  password:self.registView.passwordTF.text validCode:self.registView.verificationTF.text IDCard:self.registView.IDTF.text method:POST finish:^(NSData *data) {
            
            NSDictionary *dic1 = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSLog(@"%@",dic1);
            StringResponse *rese1 = [[StringResponse alloc]init];
            
            [rese1 dealWithData:data];
            

            
            
            if ( [rese1.Code isEqualToString:@"0"]) {
                //            注册成功 进入登陆界面
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"注册成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alert.delegate = self;
                [alert show];
                
            }
        } error:^(NSError *error) {
            ;
        }];
    } else {
         [PKRequestManager showAlertView:@"密码不一致"];
    }
}


// 查看用户协议
- (void)lookUserProtocol{

    BSUserProtocolInfromationView *UPIVC = [[BSUserProtocolInfromationView alloc]init];
//    [self.navigationController pushViewController:UPIVC animated:YES];
    [self presentViewController:UPIVC animated:YES completion:nil];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{



    [self dismissViewControllerAnimated:YES completion:^{
        nil;
    }];

}







- (void)dealloc{

   }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
